<template>
	<view>
		<view v-if='coupon != ""'>
			<view style='text-align: center;font-size: 18px;color:#606266;padding:10px'>
				{{coupon.coupon.__toString}}
			</view>
			<view style='padding: 10px'>
				<view>
					剩余核销次数：{{ coupon.remains }}
				</view>
				<view style='color:#909399;padding-top:10px;'>
					核销项目/门店：{{ coupon.coupon.__metadata.stores[0].__toString }}
				</view>
				<view style='color:#909399;padding-top:10px;'>核销开始时间:{{$u.timeFormat(new Date(coupon.coupon.__metadata.useStartTime), 'yyyy年mm月dd日')}}</view>
				<view style='color:#909399;padding-top:10px;'>核销结束时间:{{$u.timeFormat(new Date(coupon.coupon.__metadata.useEndTime), 'yyyy年mm月dd日')}}</view>
				<view v-if='coupon.code' style='color:#909399;padding-top:10px;'>
					编号：{{coupon.code}}
				</view>
				<view style='margin-top:10px'>
					<u-parse :content="coupon.coupon.__metadata.description"></u-parse>
				</view>
			</view>
			<view v-if='coupon.remains > 0' class='footer'>
				<view style='padding:10px'>
					<u-button text='确定核销' size='large' type='primary' shape='circle' @click='submit'></u-button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				coupon: '',
				id: 0,
				store: 0,
				staffName: ''
			}
		},
		onShow() {

		},
		onLoad(options) {
			console.log(options)
			if (typeof(options.scene) != 'undefined') {
				this.id = options.scene
				this.getCoupon()
			} else {
				uni.showModal({
					title: '',
					content: '获取信息失败，请重新扫码',
					showCancel: false,
					success: () => {
						uni.switchTab({
							url: '/pages/index/index'
						})
					}
				})
			}
		},
		methods: {
			getCoupon() {
				if (this.id == 0) {
					uni.showModal({
						title: '',
						content: '获取卡券信息失败！',
						success: function(data) {
							uni.switchTab({
								url: '/pages/index/index'
							})
						}
					})
				}
				uni.showLoading({
					title: '正在获取优惠券'
				})
				//先判断登录状态
				let token = uni.getStorageSync('api_token')
				if (token == '') {
					this.wxLogin()
				} else {
					this.request.get(`api/staffs`).then((res) => {
						console.log('staff',res.data)
						if(res.data && res.data.length > 0){
							let store = res.data[0].store.id
							this.store = store
							this.staffName = res.data[0].name
							this.request.get(`store/${store}/user-coupons/${this.id}`,{
								'@expands': "['coupon']"
							}).then((res) => {
								console.log(res.data)
								this.coupon = res.data
								uni.hideLoading()
								if(res.data.remains <= 0){
									uni.showModal({
										title: '',
										content: '该券已核销完毕！',
										showCancel: false,
										success: () => {
											
										}
									})
								}
							})
						}else{
							uni.hideLoading()
							uni.showModal({
								title: '',
								content: '您没有核销权限！',
								showCancel: false,
								success: () => {
									uni.switchTab({
										url: '/pages/index/index'
									})
								}
							})
						}
					})
				}
			},
			submit(){
				if(this.coupon.remains <= 0){
					uni.showModal({
						title: '',
						content: '该券已核销完毕！',
						showCancel: false,
						success: () => {
							
						}
					})
					return
				}
				uni.showModal({
					title: '',
					content: '是否确定核销？',
					success: (data) => {
						if(data.confirm){
							uni.showLoading()
							this.request.post(`store/${this.store}/user-coupons/${this.id}/verify`,{
								extra_data: {
									staff: this.staffName
								}
							}).then((res) => {
								uni.hideLoading()
								uni.showModal({
									title: '',
									content: '核销成功！',
									showCancel: false,
									success: () => {
										this.getCoupon()
									}
								})
							})
						}
					}
				})
			},
			wxLogin() {
				let _this = this;
				uni.login({
					provider: "weixin",
					success: function(res) {
						let data = {
							code: res.code
						}
						_this.request.get("wechat/mini/login", data).then((res) => {
							console.log('loginData', res)
							if (res.data.token) {
								uni.setStorageSync('api_token', res.data.token)
								_this.getCoupon()
							}
							if (res.data.data.session_key) {
								_this.sessionKey = res.data.data.session_key
							}
						})
					},
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.footer{
		position: fixed;
		bottom: 0px;
		width: 100%;
	}
</style>